#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

//int maximumAmount(vector<vector<int>>& coins) {
//    int n = coins.size(), m = coins[0].size();
//    vector<vector<vector<int>>> f(n + 1, vector<vector<int>>(m + 1, vector<int>(3, INT_MIN / 2))); \
//        f[0][1][0] = f[0][1][1] = f[0][1][2] = 0;
//    for (int i = 1; i <= n; i++)
//        for (int j = 1; j <= m; j++)
//        {
//            int x = coins[i - 1][j - 1];
//            f[i][j][0] = max(f[i - 1][j][0], f[i][j - 1][0]) + x;
//            f[i][j][1] = max({ f[i - 1][j][1] + x,f[i][j - 1][1] + x,f[i - 1][j][0],f[i][j - 1][0] });
//            f[i][j][2] = max({ f[i - 1][j][2] + x,f[i][j - 1][2] + x,f[i - 1][j][1],f[i][j - 1][1] });
//        }
//    return f[n][m][2];
//}

int minFallingPathSum(vector<vector<int>>& grid) {
    int n = grid.size(), m = grid[0].size();
    vector<vector<int>> f(n + 1, vector<int>(m + 1, INT_MAX / 2));
    for (int i = 1; i <= m; i++)   f[1][i] = grid[0][i - 1];
    for (int i = 2; i <= n; i++)
        for (int j = 1; j <= m; j++)
            for (int k = 1; k <= m; k++)
                if (j != k)
                    f[i][j] = min(f[i][j], f[i - 1][k] + grid[i - 1][j - 1]);
    int res = INT_MAX;
    for (int i = 1; i <= m; i++)
        res = min(res, f[n][i]);
    return res;
}

int main()
{
    vector<vector<int>> vv = { {7} };
    minFallingPathSum(vv);
    return 0;
}